import 'package:flutter/material.dart';
import 'package:sweet_app/pages/widgets/gradient_button.dart';

class FriendsPage extends StatefulWidget {
  @override
  _FriendsPageState createState() => _FriendsPageState();
}

class _FriendsPageState extends State<FriendsPage> {
  List<String> _friends = ["小A", "小D", "张三"];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("聊聊"),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.search),
            onPressed: () {},
          ),
          IconButton(
            icon: Icon(Icons.notifications),
            onPressed: () {},
          )
        ],
      ),
      body: ListView.builder(
        itemExtent: 50.0,
        padding: EdgeInsets.all(5.0),
        itemBuilder: _itemBuilder,
        itemCount: _friends.length,
      ),
    );
  }

  onTap(String friendName) {}

  Widget _itemBuilder(BuildContext context, int index) {
    if (index == 0) {
      return ListTile(
        leading: CircleAvatar(
            child: Icon(Icons.person_add, color: Colors.white),
            backgroundColor: Colors.orange),
        title: Text("添加朋友"),
        onTap: () => Navigator.of(context).pushNamed("/addFriend"),
      );
    }
    if (index == 1) {
      return ListTile(
          leading: CircleAvatar(
              child: Icon(Icons.people, color: Colors.white),
              backgroundColor: Colors.lightGreen),
          title: Text("群聊"));
    }
    return FriendItemWidget(
      friendName: _friends[index],
      onTap: () => onTap(_friends[index]),
    );
  }
}

class FriendItemWidget extends StatelessWidget {
  final String friendName;

  final VoidCallback onTap;

  const FriendItemWidget({this.friendName, this.onTap});

  @override
  Widget build(BuildContext context) {
    return SizedBox(
        height: 55.0,
        child: ListTile(
          leading: CircleAvatar(
            child: Text("A"),
          ),
          title: Text(friendName),
          onTap: onTap,
        ));
  }
}
